import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JTextArea;


public class KeyDemoFrame extends JFrame implements KeyListener {

    private String line1 = "";
    private String line2 ="";
    private String line3 = "";
    private JTextArea textArea;
    
    public KeyDemoFrame(){
	super("Key demo frame");
	setLayout(new FlowLayout());
	textArea = new JTextArea(10, 15);
	textArea.setText("Press any key on keyboard");
	textArea.setEnabled(false);
	textArea.setDisabledTextColor(Color.DARK_GRAY);
	add(textArea);
	addKeyListener(this);
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
	line1 = String.format("Key pressed : %s", e.getKeyText(e.getKeyCode()));
	setLine2and3(e);
	
    }

    @Override
    public void keyReleased(KeyEvent e) {
	line1 = String.format("Key relased : %s", e.getKeyCode());
	setLine2and3(e);
	clear();
	
    }

    @Override
    public void keyTyped(KeyEvent e) {
	line1 = String.format("Key pressed : %s", e.getKeyCode());
	setLine2and3(e);
	
    }
    
    private void setLine2and3(KeyEvent e){
	line2 = String.format("This key is%s action key", 
		e.isActionKey() ? "":"not" );
	String temp = e.getKeyModifiersText(e.getModifiers());
	line3 = String.format("Klawisz modyfikacji : %s",
		(temp.equals("")? "none": temp) );
	
	textArea.setText(String.format("%s\n%s\n%s", line1, line2, line3));
    }
    
    private void clear(){
	textArea.setText("");
    }

}
